package com.example.controller;

import com.example.common.Result;
import com.example.entity.Admin;
import com.example.service.AdminService;
import com.example.utils.RedisUtils;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 管理员前端操作接口
 **/
@RestController
@RequestMapping("/admin")
public class AdminController {

    private static final Logger log = LoggerFactory.getLogger(AdminController.class);
    @Resource
    private AdminService adminService;
    @Autowired
    private RedisUtils redisUtils;

    private long expireTime = 1800;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Admin admin) {
        String key = admin.getUsername() + admin.getRole();
        long startTime = System.currentTimeMillis();
        adminService.add(admin);
        long endTime = System.currentTimeMillis();
        System.out.println("更新数据库中数据所用时间：:"+(endTime - startTime));
        startTime = System.currentTimeMillis();
        redisUtils.remove(key);
        endTime = System.currentTimeMillis();
        System.out.println("清除redis中已经存在的数据用时："+(endTime - startTime));
        return Result.success();
    }

    /**
     * 删除
     */
    @PutMapping("/delete")
    public Result deleteById(@RequestBody Admin admin) {
        adminService.deleteById(admin.getId());
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Admin admin) {
        adminService.updateById(admin);
        return Result.success();
    }

    /**
     * 分页查询
     */
//    @GetMapping("/selectPage")
//    public Result selectPage(Admin admin,
//                             @RequestParam(defaultValue = "1") Integer pageNum,
//                             @RequestParam(defaultValue = "10") Integer pageSize) {
//        PageInfo<Admin> page = adminService.selectPage(admin, pageNum, pageSize);
//        System.out.println("直接查询到的page值："+page.toString());
//        return Result.success(page);
//    }

    // 接下来的一套引入redis做缓存查询
    @GetMapping("/selectPage")
    public Result selectPage(Admin admin,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Admin> page;
        String key = "AdminInfoList";
        long startTime = System.currentTimeMillis();
        long endTime;
        if(redisUtils.get(key) != null) {
            endTime = System.currentTimeMillis();
            System.out.println("查询redis是否为空所用时间："+(endTime - startTime));
            startTime = System.currentTimeMillis();
            page = (PageInfo<Admin>) redisUtils.get(key);
            System.out.println(page);
            endTime = System.currentTimeMillis();
            System.out.println("如果redis不为空时，所用的查询时间："+(endTime - startTime));
        } else {
            startTime = System.currentTimeMillis();
            page = adminService.selectPage(admin, pageNum, pageSize);
            endTime = System.currentTimeMillis();
            System.out.println("从数据库中查数据所用的时间："+(endTime - startTime));
            startTime = System.currentTimeMillis();
            redisUtils.set(key, page, expireTime);
            endTime = System.currentTimeMillis();
            System.out.println("将数据写入redis所用的时间："+(endTime - startTime));
        }
        return Result.success(page);
    }
}